package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.userInfo.adapter.CityAdapter;
import com.rongshu.view.userInfo.entity.City;
import com.rongshu.view.userInfo.entity.Province;
import com.rongshu.view.userInfo.iview.CityView;
import com.rongshu.view.userInfo.presenter.CityPresenter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class CitySelectActivity extends BaseActivity<CityPresenter> implements CityView, CityAdapter.OnItemClickListener {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    //intent
    private Province.ResponseBean responseBean = null;
    //manager
    private LinearLayoutManager manager = null;
    //adapter
    private CityAdapter adapter = null;


    @Override
    protected int initLayout() {
        return R.layout.activity_city_select_view;
    }

    @Override
    protected void initView() {
        //获取意图数据
        responseBean = getIntent().getParcelableExtra(Constant.INTENT_DATA);
        if (responseBean != null) {
            txt_top_title.setText(responseBean.getTitle());
        }
        //manger
        manager = new LinearLayoutManager(this);
        //presenter
        setPresenter(new CityPresenter(this), this);
        //adapter
        adapter = new CityAdapter(this);
        adapter.setOnItemClickListener(this);
        adapter.setHasStableIds(true);
        //recyclerview
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {
        //获取省份
        Map<String, Object> map = new HashMap<>();
        map.put("parent_id", responseBean.getId());
        presenter.getCity(
                (int) map.get("parent_id"),
                Common.encryptionSign(map));
    }

   @OnClick({R.id.layout_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
        }
    }

    /**
     * 返回城市数据
     *
     * @param responseBeanList
     */
    @Override
    public void onGetCity(List<City.ResponseBean> responseBeanList) {
        if (adapter != null) {
            adapter.setResponseBeans(responseBeanList);
            adapter.notifyDataSetChanged();
        }
    }

    /**
     * item
     *
     * @param position
     */
    @Override
    public void onItemClick(int position) {
        if (adapter != null) {
            String title = adapter.getResponseBeans().get(position).getTitle();
            if (responseBean != null) {
                Intent intent = new Intent();
                intent.putExtra(Constant.INTENT_MESSAGE, responseBean.getTitle() + " " + title);
                setResult(RESULT_OK,intent);
                finish();
            }
        }
    }
}
